var gulp = require('gulp'),
  sass = require("gulp-sass"),
  config = require("./config.js"),
  sourcemaps = require('gulp-sourcemaps'),//映射文件
  browserSync = require("browser-sync").create(),
  reload = browserSync.reload,
  SSI = require('browsersync-ssi'),
  filter = require('gulp-filter'),
  //用来编译sass
  sass = require('gulp-sass'),
  cssAutoprefixer = require('gulp-autoprefixer'),
  cleanCSS = require('gulp-clean-css'),//压缩css
  rev = require("gulp-rev"),
  revCollector = require('gulp-rev-collector'),
  // 需要ruby 不用了 compass = require('gulp-compass'),
  //合并文件
  concat = require('gulp-concat'),

  //混淆js压缩
  uglify = require('gulp-uglify'),
  //压缩js
  minify = require('gulp-minify'),

  //错误处理插件plumber
  plumber = require('gulp-plumber'),
  //clean 用来删除文件
  clean = require('gulp-clean'),
  //压缩文件
  zip = require('gulp-zip'),
  filesCopy = require("gulp-copy"),
  runSequence = require('gulp-run-sequence');


gulp.task("serve",function(){
  browserSync.init({
    server:{
      baseDir:config.dest
    }
  });
  gulp.watch([config.sass.all],["sass"]);
  gulp.watch([config.duplicateHtml.all],["duplicateHtml"]);
  gulp.watch([config.duplicateJs.all],["duplicateJs"]);
  gulp.watch([config.duplicateImg.all],["duplicateImg"]);
  gulp.watch([config.duplicateComponents.all],["duplicateComponents"]);
})


gulp.task("sass", function () {
  return gulp.src(config.sass.all).pipe(plumber()).pipe(sass({
    //设置生成sourcemap，在调试器中显示样式在scss文件中的位置，便于调试
    sourcemap: 'true',
    //输出格式设置为compressed就不需要压缩css了
    style: 'compressed'
    //文件目录
    // css: 'app/css',
    // sass: 'app/sass',
    // image: 'app/img'
  }))//如果有错误输出错误提示
    .on('error', function (error) {
      // Would like to catch the error here
      console.log(error);
      this.emit('end');
    })
    .pipe(gulp.dest(config.sass.dest)).pipe(reload({stream: true}));
});

gulp.task("duplicateHtml", function () {
  return gulp.src([config.duplicateHtml.all,"!"+ config.duplicateStaticLib.all]).pipe(gulp.dest(config.duplicateHtml.dest)).pipe(reload({stream:true}));
});

gulp.task("duplicateJs", function(){
  return gulp.src([config.duplicateJs.all]).pipe(gulp.dest(config.duplicateJs.dest)).pipe(reload({stream:true}));
});

gulp.task("duplicateImg", function(){
  return gulp.src([config.duplicateImg.all]).pipe(gulp.dest(config.duplicateImg.dest)).pipe(reload({stream:true}));
});

gulp.task("duplicateComponents", function(){
  return gulp.src([config.duplicateComponents.all,"!"+config.duplicateStaticLib.all]).pipe(gulp.dest(config.duplicateComponents.dest)).pipe(reload({stream:true}));
});

gulp.task("duplicateStaticLib", function(){
  return gulp.src([config.duplicateStaticLib.all]).pipe(gulp.dest(config.duplicateStaticLib.dest));
});

gulp.task("cleanDist", function(){
  return gulp.src([config.dest], {read: false}).pipe(clean({force: true}));
});

gulp.task("redist", function () {
  runSequence("cleanDist", "duplicateHtml", "duplicateJs", "duplicateImg","duplicateComponents","sass","duplicateStaticLib");
});

gulp.task("default",function(){
  runSequence('redist','serve');
})


//////////////////////////////////////////////////////// release /////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////// release /////////////////////////////////////////////////////////////////////////

gulp.task("serve-release",function(){
  browserSync.init({
    server:{
      baseDir : config.dest
    }
  });
  gulp.watch([config.sass.all],["sass-release"]);
  gulp.watch([config.duplicateHtml.all],["duplicateHtml-release"]);
  gulp.watch([config.duplicateJs.all],["duplicateJs-release"]);
  gulp.watch([config.duplicateImg.all],["duplicateImg-release"]);
  gulp.watch([config.duplicateComponents.all],["duplicateComponents-release"]);
  gulp.watch([config.duplicateStaticLib.all],["duplicateStaticLib-release"]);
});

gulp.task("sass-release", function(){
  return gulp.src(config.sass.all).pipe(plumber()).pipe(sass({
    //设置生成sourcemap，在调试器中显示样式在scss文件中的位置，便于调试
    sourcemap: 'true',
    //输出格式设置为compressed就不需要压缩css了
    style: 'compressed'
    //文件目录
    // css: 'app/css',
    // sass: 'app/sass',
    // image: 'app/img'
  }))//如果有错误输出错误提示
    .on('error', function(error){
      // Would like to catch the error here
      console.log(error);
      this.emit('end');
    })
    .pipe(cssAutoprefixer())
    .pipe(cleanCSS())
    .pipe(rev())
    .pipe(gulp.dest(config.sass.dest))
    .pipe(rev.manifest())
    .pipe(gulp.dest(config.dest + 'revCssJson'));
    //.pipe(reload({stream: true}));
});

gulp.task("duplicateJs-release", function(){
  return gulp.src([config.duplicateJs.all])
    .pipe(plumber())
    .pipe(uglify())
    .pipe(minify())
    .pipe(rev())
    .pipe(gulp.dest(config.duplicateJs.dest))
    .pipe(rev.manifest())
    .pipe(gulp.dest(config.dest+ "revJsJson"));
});

gulp.task("duplicateImg-release", function(){
  return gulp.src([config.duplicateImg.all])
    .pipe(plumber())
    .pipe(rev())
    .pipe(gulp.dest(config.duplicateImg.dest))
    .pipe(rev.manifest())
    .pipe(gulp.dest(config.dest+"revImgJson"));
});

gulp.task("duplicateHtml-release",function(){
  return gulp.src([config.duplicateHtml.all ,"!"+config.duplicateStaticLib.all,config.dest + 'revCssJson/*',config.dest+ "revJsJson/*",config.dest+"revImgJson/*"])
    .pipe(plumber())
    .pipe(revCollector())
    .pipe(gulp.dest(config.duplicateHtml.dest));
});

gulp.task("duplicateComponents-release", function(){
  return gulp.src([config.duplicateComponents.all,"!"+config.duplicateStaticLib.all,config.dest + 'revCssJson/*',config.dest+ "revJsJson/*",config.dest+"revImgJson/*"])
    .pipe(plumber())
    .pipe(revCollector())
    .pipe(gulp.dest(config.duplicateComponents.dest));
});

gulp.task("duplicateStaticLib-release", function(){
  return gulp.src([config.duplicateStaticLib.all]).pipe(gulp.dest(config.duplicateStaticLib.dest));
});

gulp.task("cleanDist-release", function(){
  return gulp.src([config.dest], {read: false}).pipe(clean({force: true}));
});

gulp.task("redist-release", function () {
  runSequence("cleanDist-release", "sass-release","duplicateJs-release", "duplicateImg-release","duplicateHtml-release", "duplicateComponents-release","duplicateStaticLib-release");
});

gulp.task("default-release",function(){
  runSequence("redist-release",["serve-release"]);
});

